package com.example.administrator.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2016/4/9 0009.
 */
public class ImageAndText extends View {
    private Bitmap mBitmap;
    private Paint mPaint;
    private int mWidth,mHeight;
    public ImageAndText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.imageAndText);
        Drawable drawable = typedArray.getDrawable(R.styleable.imageAndText_imgSrc);

        mBitmap = ((BitmapDrawable)drawable).getBitmap();
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawColor(Color.LTGRAY);
        //计算图片压缩的比例
        float sX = mWidth/(float)mBitmap.getWidth();
        float sY = mHeight/(float)mBitmap.getHeight();
        float scale = Math.max(sX, sY);
        mBitmap = Bitmap.createScaledBitmap(mBitmap, (int)(mBitmap.getWidth()*scale), (int)(mBitmap.getHeight()*scale), true);
        //新建图层
        int sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), mPaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRect(0,0,mWidth, mHeight, mPaint);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        canvas.drawBitmap(mBitmap, 0, 0, mPaint);
        mPaint.setTextSize(20);
        canvas.drawText("我是图片",mWidth/2,mHeight/2,mPaint);
        mPaint.setXfermode(null);

        canvas.restoreToCount(sc);
    }

    /*
     * 一个控件多大？
     * 自己想要多大
     * 父容器
     */
    //对控件的大小进行测量
    /*
     * 测量的模式：
     *  MeasureSpec.UNSPECIFIED  表示未指定   父容器不对控件做出限制，你想要多大都可以
     *  MeasureSpec.EXACTLY   表示完全的      控件有多大，父容器已经算好
     *  MeasureSpec.AT_MOST  表示最多  父容器已经给该控件设定了最大值，该控件最大不能超过这个值
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        //得到宽度测量的Mode
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        //获得宽度的值
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (widthMode)
        {
            case MeasureSpec.UNSPECIFIED:
                mWidth = mBitmap.getWidth();
                break;
            case MeasureSpec.EXACTLY:
                mWidth = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                mWidth = Math.min(mBitmap.getWidth(), widthSize);
                break;
        }

        //得到宽度测量的Mode
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获得宽度的值
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (heightMode)
        {
            case MeasureSpec.UNSPECIFIED:
                mHeight = mBitmap.getHeight();
                break;
            case MeasureSpec.EXACTLY:
                mHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                mHeight = Math.min(mBitmap.getHeight(), heightSize);
                break;
        }

        //设置测量结果
        setMeasuredDimension(mWidth, mHeight);
    }
}
